$(document).ready(function(){
	initGoods();
})

/**
 * 初始化商品
 */
function initGoods(){
	var url ='goodsController.do?method=listData';
	dataTable({
		tableId:'allGoodsList',
		title:'商品列表',
		url:url,
		idField:'id',
		columns:[[
		          {field : 'ids',checkbox : true,align : 'center'},
                  {title : '商品名称',field : 'name',width : 100},
		          {title : '库存量',field : 'repertory',width : 100},
		          {title : '进货日期',field : 'purchaseDateStr',width : 150},
		          {title : '原价',field : 'originalPrice',width : 100},
		          {title : '现价',field : 'makerPrice',width : 100},
		 ]],
	  showPage:true,
	  toolbar:'toolbar'
   });
}

//返回商品列表
$('#returnGood').click(function(){
	initGoods();
})

//修改商品
$("#updateGood").click(function(){
	   var rows = $("#allGoodsList").datagrid("getChecked");
	   var len = rows.length;
	   if(len<1){
	   	  $.messager.alert("系统提示", "请选择要修改的商品！","warning");
	   	  return;
	   }else if(len>1){
	   	  $.messager.alert("系统提示", "每次只能修改一个商品，请重新选择！","warning");
	   	  return;
	   }
	   updateRowData(rows[0].id);   
	});

//修改商品
function updateRowData(menuId){ 
	{  
		   var url = "goodsController.do?method=index&type=update";
		   url = urlAddParmert(url,"iaId",menuId); 
		   //打开easyui窗口
		   easyuiWidow({
		   	 title:'修改商品',
		     winId:'updateWin',
		     width:'400',
		     height:'350',
		     iconCls:'icon-win-addmenu',
		     url:url
		   });
		}
}

//删除商品
$("#deleteGoods").click(function(){
	var rows = $('#allGoodsList').datagrid('getSelections');
	var len = rows.length;
	if(len==0){
    $.messager.alert("系统提示","请选择要删除的商品","warning");
    return;
 }
	var ids = new Array();
	for(var i = 0;i<len;i++){     
 	  ids.push(rows[i].id);
 }
 deleteRowData(ids);
});
//删除商品
function deleteRowData(menuIds){   
   var len = 1;
   var ids = new Array();
   if(typeof(menuIds)!='string'){
     len = menuIds.length;
     ids = menuIds;
   }else{
     ids.push(menuIds);
   }
   var url = "goodsController.do?method=deleteGoods";
 //删除商品
   deleteData({
     len:len,
     url:url,
     params:{'id':ids,'version':Math.random()},
     dataType:'text',
     callback:function(msg){
        if(msg=="EMPTY"){
           $.messager.alert("系统提示","请选择要删除的商品","warning");
        }else if(msg=="FAIL"){
           $.messager.alert("系统提示","删除商品失败","warning");
        }else if(msg=="SUCCESS"){
           $.messager.alert("系统提示","删除商品成功","warning");
           reloadTable({tableId:'allGoodsList'});		   
        }else{
            $.messager.alert("系统提示","系统出现异常："+msg,"error");
        }
     }
   });
}
//添加商品
$("#addGood").click(function(){
	var url = "goodsController.do?method=index&type=add";
    easyuiWidow({
	title:'添加商品',
	winId:'addWin',
	width:'400',
	height:'350',
	iconCls:'icon-win-addmenu',
	url:url
   });
});


//查找商品
$("#search_TGoods").click(function(){
	var name = $("#name").val();
	var purchaseBeginDate = $("#purchaseBeginDate").val();
	var purchaseEndDate = $("#purchaseEndDate").val();
	var url = "goodsController.do?method=listData";
	url = urlAddParmert(url,"name",encodeURI(name));
	url = urlAddParmert(url,"purchaseBeginDate",encodeURI(purchaseBeginDate));
	url = urlAddParmert(url,"purchaseEndDate",encodeURI(purchaseEndDate));

	dataTable({
		url: url,
		showPage:true,
		tableId:'allGoodsList',		  
		toolbar:'toolbar'
	});
	reloadTable({tableId:'allGoodsList'});	
});
